﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SmartMotion.Core.Mgrs;
using Sunny.UI;

namespace SmartMotion.Core
{
    public partial class FrmActionItem : UIPage
    {
        private readonly ActionItem _actionItem;
        private BindingList<MethodParms> _bingList;
        private UITextBoxTree _textBoxTree;
        public FrmActionItem(ActionItem actionItem)
        {
            _actionItem = actionItem;
            InitializeComponent();
        }

        private void FrmActionItem_Load(object sender, EventArgs e)
        {
            #region 初始化表格部分

            SetGrid();

            SetTextBoxTree();

            grid.Controls.Add(_textBoxTree);

            #endregion


            #region 底部控件

            lblStatic.Visible = _actionItem.IsStatic;
            lblObj.Visible = !_actionItem.IsStatic;
            cmbObj.Visible = !_actionItem.IsStatic;
            VarMgr.Ins.ComboTreeLoadData(cmbObj,typeof(object).FullName);
            cmbObj.Text = _actionItem.ObjName;

            lblReturn.Visible =! _actionItem.ReturnType.Contains("Void");
            lblType.Visible = lblReturn.Visible;
            cmbReturn.Visible = lblReturn.Visible;
            VarMgr.Ins.VarTreeLoadData(cmbReturn, new List<string>(){ _actionItem.ReturnType ,typeof(object).FullName} );
            cmbReturn.Text = _actionItem.ReturnObjName;

            #endregion

        }

        #region Grid

        private void SetGrid()
        {
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            grid.RowHeadersVisible = false;
            grid.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            grid.AllowUserToResizeRows = false;
            grid.MultiSelect = false;
            grid.AllowUserToAddRows = false;
            grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            _bingList = new BindingList<MethodParms>(_actionItem.Parms);
            grid.DataSource = _bingList;

            foreach (DataGridViewColumn dataGridViewColumn in grid.Columns)
            {
                dataGridViewColumn.ReadOnly = true;
            }

            if (grid.Columns.Count == 3)
            {
                grid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                grid.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                grid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
        }

        private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (grid.CurrentRow==null)
            {
                _textBoxTree.Visible = false;
                return;
            }

            if (grid.CurrentCell.ColumnIndex!=2)
            {
                _textBoxTree.Visible = false;
                return;
            }

            _textBoxTree.ButtonTreeView.Nodes.Clear();
            _textBoxTree.TextBoxTreeView.Nodes.Clear();
            var type = grid.CurrentRow.Cells[1].Value;
            string varType = type.ToString();
            VarMgr.Ins.VarTreeLoadData(_textBoxTree,varType);

            int columnIndex = grid.CurrentCell.ColumnIndex;
            int rowIndex = grid.CurrentCell.RowIndex;
            var rect = grid.GetCellDisplayRectangle(columnIndex, rowIndex, false);
            _textBoxTree.Left = rect.Left;
            _textBoxTree.Top = rect.Top;
            _textBoxTree.Width = rect.Width;
            _textBoxTree.Height = rect.Height;
            if (null!=grid.Rows[rowIndex].Cells[columnIndex].Value)
            {
                _textBoxTree.Text = grid.Rows[rowIndex].Cells[columnIndex].Value.ToString();
            }
            else
            {
                _textBoxTree.Text = string.Empty;
            }

            _textBoxTree.Visible = true;
            _textBoxTree.Focus();
        }


        #endregion

        #region TextBoxTree

        private void SetTextBoxTree()
        {
            _textBoxTree = new UITextBoxTree();
            _textBoxTree.Visible = false;
            _textBoxTree.ButtonTreeNodeMouseClick += _textBoxTree_ButtonTreeNodeMouseClick; //变量选择事件
            _textBoxTree.TextBoxTreeNodeMouseClick += _textBoxTree_TextBoxTreeNodeMouseClick; //数组索引被选择事件
            _textBoxTree.KeyUp += _textBoxTree_KeyUp;
            _textBoxTree.TextAlignment = ContentAlignment.MiddleLeft;
        }

        private void _textBoxTree_KeyUp(object sender, KeyEventArgs e)
        {
            grid.CurrentCell.Value = _textBoxTree.Text;
            _actionItem.Parms[grid.CurrentCell.RowIndex].VarSelected = false;
        }

        private void _textBoxTree_TextBoxTreeNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string text = _textBoxTree.Text;
            _textBoxTree.Text = text.Left(_textBoxTree.SelectionStart) +
                                e.Node.FullPath.Replace("\\", ".") +
                                text.Right(_textBoxTree.Text.Length - _textBoxTree.SelectionStart);
            grid.CurrentCell.Value = _textBoxTree.Text;
            _textBoxTree.Visible = false;
            _actionItem.Parms[grid.CurrentCell.RowIndex].IndexSelected = true;
        }

        private void _textBoxTree_ButtonTreeNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string fullPath = e.Node.FullPath.Replace("\\", ".")/*+"[]"*/;
            _textBoxTree.Text = fullPath;
            grid.CurrentCell.Value = fullPath;
            _actionItem.Parms[grid.CurrentCell.RowIndex].VarSelected = true;
            _textBoxTree.Visible = false;
        }

        #endregion

        #region 底部下拉被选择

        private void cmbObj_NodeSelected(object sender, TreeNode node)
        {
            _actionItem.ObjName = node.Text;
            _actionItem.ObjGuid = node.Tag.ToString();
        }

        private void cmbReturn_ButtonTreeNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string fullPath = e.Node.FullPath.Replace("\\", ".") /*+ "[]"*/;
            cmbReturn.Text = fullPath;
            _actionItem.ReturnObjName = fullPath;
        }

        #endregion
    }
}
